﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
/* CarState properties
 * 
 * string   CarId
 * double   LocationX
 * double   LocationY
 * double   Speed
 * double   OilPressure
 * double   GasPressure
 * DateTime Time
 */
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// The park point.
    /// Operations with data base.
    /// </summary>
    public class ParkPoint : DalEntity
    {
        /// <summary>
        /// The DB - DAL properties mapping.
        /// Item1 - name,
        /// Item2 - dbName,
        /// Item3 - sqlParam,
        /// Item4 - type,
        /// Item5 - size
        /// </summary>
        private static List<Tuple<string, string, string, SqlDbType, int>> properties; 

        static ParkPoint()
        {
            properties = new List<Tuple<string, string, string, SqlDbType, int>>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("C:\\Users\\Robert Sargsyan\\Documents\\Visual Studio 2012\\Projects\\Tracking\\ParkPointArgs.xml");
            foreach (XmlNode arg in xmlDoc.DocumentElement)
            {
                properties.Add(new Tuple<string, string, string, SqlDbType, int>(arg.Attributes["name"].Value, arg.Attributes["dbName"].Value,
                    arg.Attributes["sqlParam"].Value,
                    (SqlDbType)Enum.Parse(typeof(SqlDbType), arg.Attributes["type"].Value),
                    Convert.ToInt32(arg.Attributes["size"].Value)));
            }
        }

        /// <summary>
        /// Returns a reader on all park points.
        /// Argument is the park id.
        /// </summary>
        public override Reader ReadAll(Dictionary<string, object> args)
        {
            ValidateArguments(args, "Id");
            SqlCommand command;
            if ((int)args["Id"] < 0)
                command = new SqlCommand("ReadAllParkPoints");
            else
            {
                command = new SqlCommand("ReadParkPointsByParkId");
                SqlParameter id = command.Parameters.Add("@parkId", SqlDbType.Int);
                id.Direction = ParameterDirection.Input;
                id.Value = args["Id"];
            }
            Reader reader = new Reader(command, properties);

            return reader;
        }

        /// <summary>
        /// Not used.
        /// </summary>
        public override Dictionary<string, object> ReadById(Dictionary<string, object> args)
        {
            throw new MethodAccessException("Can't read park point by id");
        }

        /// <summary>
        /// Returns empty ParkPoint.
        /// </summary>
        public override Dictionary<string, object> Create()
        {
            Dictionary<string, object> parkPoint = new Dictionary<string, object>();
            for (int i = 0; i < properties.Count; i++)
            {
                parkPoint[properties[i].Item1] = null;
            }
            return parkPoint;
        }

        /// <summary>
        /// Writes the park point in DB
        /// </summary>
        public override bool? Write(Dictionary<string, object> args)
        {
            _write("WriteParkPoint", args, properties);

            return true;
        }

        /// <summary>
        /// Removes park point with specified index and parkId.
        /// If index not specified, removes all points of the park.
        /// </summary>
        public override bool? Remove(Dictionary<string, object> args)
        {
            ValidateArguments(args, "ParkId");
            SqlCommand command;
            if (args.ContainsKey("Index"))
            {
                command = new SqlCommand("RemovePointOfPark");
                SqlParameter parkId = command.Parameters.Add("@parkId", SqlDbType.Int);
                SqlParameter index = command.Parameters.Add("@index", SqlDbType.Int);
                parkId.Direction = ParameterDirection.Input;
                index.Direction = ParameterDirection.Input;
                parkId.Value = args["ParkId"];
                index.Value = args["Index"];
            }
            else
            {
                command = new SqlCommand("RemoveAllPointsOfPark");
                SqlParameter parkId = command.Parameters.Add("@parkId", SqlDbType.Int);
                parkId.Direction = ParameterDirection.Input;
                parkId.Value = args["ParkId"];
            }

            using (SqlConnection connection = Reader.CreateConnection(command))
            {
                connection.Open();
                command.ExecuteNonQuery();
            }

            return true;
        }
    }
}
